package com.jzr.task.controller.api;

import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.task.base.JobMap;
import com.jzr.task.job.BaseJob;
import com.jzr.task.db1.model.TSysJob;
import com.jzr.task.db1.service.TSysJobService;
import com.jzr.task.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ScheduledFuture;

@RestController
@RequestMapping("/api/job")
public class JobController {
    Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TSysJobService sysJobService;

    @Autowired
    ThreadPoolTaskScheduler scheduler;

    @GetMapping("/start")
    public Result<Boolean> start(@RequestParam Integer id){
        try{
            if(U.isBlank(id)){
                return Result.fail("参数\"id\"不能为空");
            }
            if(JobMap.isRunning(id.toString())){
                return Result.fail("正在运行中");
            }
            TSysJob job = sysJobService.getById(id);
            if(U.isBlank(job)){
                return Result.fail("没有对应的JOB设置");
            }
            BaseJob baseJob = null;
            try {
                baseJob = (BaseJob) SpringUtil.getBean(job.getJobClass());
            }catch (Exception e){
                return Result.fail("实例化\"" + job.getJobClass() + "\"出错,"+e.getMessage());
            }
            if(baseJob == null){
                return Result.fail("无法实例化\"" + job.getJobClass() + "\"");
            }
            baseJob.setJobId(id);
            ScheduledFuture<?> future = scheduler.schedule(baseJob, new CronTrigger(job.getCron()));
            JobMap.add(id.toString(), future);

            //保存信息
            TSysJob tmpJob = new TSysJob();
            tmpJob.setId(id);
            tmpJob.setIsDone(false);
            tmpJob.setIsCancelled(false);
            sysJobService.save(tmpJob);

            //baseJob.run();

            return Result.success("启动成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("启动失败,"+e.getMessage());
        }
    }

    @GetMapping("/stop")
    public Result<Boolean> stop(@RequestParam Integer id, @RequestParam Boolean isForceStop){
        try {
            if(U.isBlank(id)){
                Result.fail("参数\"id\"不能为空");
            }
            if(isForceStop == null){
                isForceStop = false;
            }
            JobMap.remove(id.toString(),isForceStop);
            TSysJob tmpJob = new TSysJob();
            tmpJob.setId(id);
            tmpJob.setIsDone(true);
            tmpJob.setIsCancelled(false);
            sysJobService.save(tmpJob);

            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("失败," + e.getMessage());
        }

    }

    @GetMapping("/list")
    public List<TSysJob> list(){
        List<TSysJob> list = JobMap.getJobList();
        return list;
    }

}
